//
//  TouchIDManager.m
//  touch
//
//  Created by guomianxiang on 17/3/10.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "TouchIDManager.h"

@interface TouchIDManager ()
//@property (assign, nonatomic, readwrite) BOOL canUseTouchID;
@property (strong, nonatomic) LAContext *context;

@end


@implementation TouchIDManager

+ (TouchIDManager *)sharedTouchIDManager {
    
    static TouchIDManager *IDManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        IDManager = [[TouchIDManager alloc]init];
    });
    return IDManager;
}

- (instancetype)init {
    if (self = [super init]) {
        NSError *error;
        _canUseTouchID = [self canUseTouchIDWithError:&error];
        //LAErrorTouchIDLockout ID被锁定 默认ID可用 其他情况默认ID不可用。
        if (error.code ==
            LAErrorTouchIDLockout) {
            
            _canUseTouchID = YES;
        }
    }
    return self;
}

- (LAContext *)context {
    if (_context == nil) {
        _context = [[LAContext alloc]init];
    }
    return _context;
}

- (BOOL)canUseTouchIDWithError:(NSError *__autoreleasing *)error {
    return [self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:error];
}

- (void)touchIDLoginSuccessHandler:(void(^)())successHandler failHandler:(void(^)(NSError * _Nullable error))failHandler {
    [self validateTouchIDWithLocalizedReason:@"验证已有指纹登陆" localizedFallbackTitle:@"使用密码" successHandler:^{
        dispatch_async(dispatch_get_main_queue(), ^{
            successHandler();
        });

    } failHandler:^(NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            failHandler(error);
            
        });
    }];
}

- (void)validateTouchIDWithLocalizedReason:(NSString *)reason localizedFallbackTitle:(NSString *)fallBackTitle successHandler:(void(^)())successHandler failHandler:(void(^)(NSError * _Nullable error))failHandler {
    
    NSError *error;
    LAContext *context = [[LAContext alloc]init];
    context.localizedFallbackTitle = fallBackTitle;
    
    if ([context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        
        [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {
            if (success) {
                successHandler();
            }else {
                failHandler(error);
            }
        }];
        
    }else {
        failHandler(error);
    }
    
}

- (void)touchIDWithLocalizedReason:(NSString *)reason localizedFallbackTitle:(NSString *)title reply:(void(^)(BOOL success, NSError * _Nullable error))reply {
    
    NSError *error;
    
    if ([self.context canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:&error]) {
        
        self.context.localizedFallbackTitle = title;
        
        [self.context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:reason reply:^(BOOL success, NSError * _Nullable error) {
            

            dispatch_async(dispatch_get_main_queue(), ^{
                reply(success,error);
            });
        }];
    }else {
        reply(NO,error);
    }
}

@end
//            LAErrorAuthenticationFailed = kLAErrorAuthenticationFailed,用户没有提供有效的指纹

//            LAErrorUserCancel = kLAErrorUserCancel,用户点击了取消按钮

//            LAErrorUserFallback = kLAErrorUserFallback,用户点击了fallbacktitle按钮

//            LAErrorSystemCancel = kLAErrorSystemCancel,用户切换了app导致本app挂起

//            LAErrorPasscodeNotSet = kLAErrorPasscodeNotSet,用户未设置密码

//            LAErrorTouchIDNotAvailable = kLAErrorTouchIDNotAvailable,该设备不支持

//            LAErrorTouchIDNotEnrolled = kLAErrorTouchIDNotEnrolled,未录入指纹

//            LAErrorTouchIDLockout = kLAErrorTouchIDLockout,指纹被锁定

//            LAErrorAppCancel = kLAErrorAppCancel,app被用户不可控的挂起，例如来电话

//            LAErrorInvalidContext = kLAErrorInvalidContext context被释放了，无效
